package com.sch.www.sys.services.impl;

import com.sch.www.common.base.BaseServicesSimpleImp;
import com.sch.www.common.base.CustomPage;
import com.sch.www.sys.orm.dao.manual.AccountMapper;
import com.sch.www.sys.orm.entity.manual.Account;
import com.sch.www.sys.orm.entity.manual.AccountExample;
import com.sch.www.sys.services.AccountServices;
import com.sch.www.sys.services.AccountToRoleServices;
import com.sch.www.sys.services.RoleServices;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class AccountServicesImpl extends BaseServicesSimpleImp<Account, AccountExample, AccountMapper, AccountServices> implements AccountServices {
    @Autowired
    RoleServices roleServices;
    @Autowired
    AccountToRoleServices accountToRoleServices;


    @Override
    protected String getTableName() {
        return "";
    }

    @Override
    protected AccountExample getExampleNewInstance() {
        return new AccountExample();
    }

    @Override
    public Account selectByUsername(String username) {
        AccountExample example = getExampleNewInstance();
        example.createCriteria().andUsernameEqualTo(username);
        return getMapper().selectOneByExample(example);
    }

    @Override
    public CustomPage<Account> list(String username, String name, String idCard, Byte active, int pageNum, int pageSize) {
        AccountExample accountExample = getExampleNewInstance();
        AccountExample.Criteria criteria = accountExample.createCriteria();

        if (StringUtils.isNoneBlank(username)) {
            criteria.andUsernameLike(likePacking(username));
        }
        if (StringUtils.isNoneBlank(name)) {
            criteria.andNameLike(likePacking(name));
        }
        if (StringUtils.isNoneBlank(idCard)) {
            criteria.andIdCardLike(likePacking(idCard));
        }
        if (active!=null) {
            criteria.andActiveEqualTo(active);
        }
        CustomPage<Account> page = getMapper().selectByExampleStartPage(accountExample, pageNum, pageSize);
        return page;
    }

    @Transactional
    @Override
    public void insert(Account account, List<String> roleIdList) {
        insertSelective(account);
        accountToRoleServices.insert(account.getId(), roleIdList);
    }
}
